import { formatGMTDate, getYMD, getNowFormatDate, getDay } from '../../../../assets/js/util.js'
export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            ruleForm: {
                date: '',
            },
            /*=========时间段配置=========*/
            pickerOptions: {
                shortcuts: [{
                    text: '近7天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '近15天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '近30天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            title: '积分墙统计',
            activeIndex: "-1",
            /*=================表1数据=================*/
            tableHead1: [{
                    "prop": "logday",
                    "label": "日期"
                },
                {
                    "prop": "kindtype",
                    "label": "分类"
                },
                {
                    "prop": "source",
                    "label": "来源"
                },
                {
                    "prop": "active",
                    "label": "激活数"
                },
                {
                    "prop": "newMac",
                    "label": "新增设备数"
                }
            ],
            loading: false,
            searchable: false,
            tableData1: [],
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
        }
    },
    computed: {

    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        dateChange(val) {

        },
        /*========================页码切换刷新===============================*/
        handleSizeChange(val) {
            this.pageSize = val;
            this.getTableData()
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getTableData()
        },
        tableControl() {
            this.tableData1 = this.tableData1.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
        },
        /*=========================页面数据加载loadding================================*/
        loads() {
            this.loading = !this.loading; //加载中
            this.searchable = !this.searchable; //查询按钮不可用
        },
        /*====================时间格式化转换函数============================*/
        formatDate(date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        /*==================获取表格数据========================*/
        getTableData() {
            this.loads();
            console.log(this.ruleForm.date);
            let date1 = this.formatDate(this.ruleForm.date[0]);
            let date2 = this.formatDate(this.ruleForm.date[1]);
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`, {
                params: { 'ps': `sday=${date1}&eday=${date2}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                this.loads();
                if (parseInt(data['res']) === 0) {
                    this.tableData1 = data.warningDatas;
                    this.dataTotal = data.warningDatas.length;
                    this.tableControl();
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
        /*===========================查询功能===============================*/
        search() {
            this.loads();
            let date1 = this.formatDate(this.ruleForm.date[0]);
            let date2 = this.formatDate(this.ruleForm.date[1]);
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`, {
                params: { 'ps': `sday=${date1}&eday=${date2}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                this.loads();
                if (parseInt(data.res) === 0) {
                    if (data.length == 0) {
                        this.errorFun('查询结果为空！')
                    } else {
                        this.tableData1 = data.warningDatas;
                        this.dataTotal = data.warningDatas.length;
                        this.tableControl();
                    }
                } else {
                    this.errorFun(data.msg)
                }
            })
        },
        // 表格下载
        excel(tableIndex, sheet) {
            var that = this;
            that.xlsxTitle = sheet+"-"+this.formatDate(that.ruleForm.date[0])+"到"+this.formatDate(that.ruleForm.date[1]);
            that.downloadExl(that.tableHead1, that.tableData1,that.xlsxTitle,sheet);
            
        },
    },
    mounted() {
        // 初始化URL
        this.SRC = this.$store.getters.src;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
        this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
        /*==============默认展示最近一天数据================*/
        this.ruleForm.date = [new Date(new Date().getTime() - 1 * 86400000), new Date()];
        this.getTableData();
    }
}